package com.metaera.app.data.network.interceptor;

import com.metaera.app.BuildConfig;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.MetaEraApplication;
import com.umeng.message.PushAgent;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 公共参数
 */
public class CommonParameterInterceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        Request.Builder requestBuilder = addHeaders(originalRequest);
        return chain.proceed(requestBuilder.build());
    }


    private Request.Builder addHeaders(Request request) {
        Request.Builder requestHeader = request.newBuilder()
                .header("os", "android")
                // cn-简体中文；en-英语；jpn-日本；tc-繁体中文（默认cn）
                .header("language", "cn")
                .header("version-name", BuildConfig.VERSION_NAME)
                .header("version-code", String.valueOf(BuildConfig.VERSION_CODE));

        // 登录 token
        String userToken = LocalConfigStore.getInstance().getUserToken();
        if (userToken != null && userToken.length() > 0){
            requestHeader.header("Authorization", userToken);
        }

        // 设备唯一ID
        String deviceId = PushAgent.getInstance(MetaEraApplication.sInstance).getRegistrationId();
        if (deviceId != null){
            requestHeader.header("device-id", deviceId);
        }

        return requestHeader;
    }

}

